package cn.fl.ref.fncprjpaysch.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itextpdf.text.pdf.PdfStructTreeController.returnType;

import tc.esign.facade.esign.api.IFilSignApiService;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.fncprjpaysch.service.IFncPrjPayInsService;
import cn.fl.ref.fncprjpaysch.transform.FncPrjPayInsVOTransform;
import cn.fl.ref.fncprjpaysch.vo.FncPrjPayInsVO;
import fl.ref.fncprjpaysch.condition.FncPrjPayInsQueryCondition;
import fl.ref.fncprjpaysch.constant.FncPrjPaySchConstant;
import fl.ref.fncprjpaysch.dto.FncPrjPayInsDTO;
import fl.ref.fncprjpaysch.service.IFncPrjPayInsApiService;

/**
 * @Description: //再融资支付表机构关系   客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class FncPrjPayInsServiceImpl implements IFncPrjPayInsService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IFncPrjPayInsService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IFncPrjPayInsApiService fncPrjPayInsApiService;
	@Autowired
	private IFilSignApiService filSignApiService;
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	
	@Override
	public FncPrjPayInsVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		FncPrjPayInsDTO dto = this.fncPrjPayInsApiService.selectById(id);
		return FncPrjPayInsVOTransform.toVO(dto);
	}

	@Override
	public Long add(FncPrjPayInsVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.fncPrjPayInsApiService.add(FncPrjPayInsVOTransform.toDTO(entity));
		//return this.fncPrjPayInsApiService.addByPrj(entity.getFncPrjId());
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.fncPrjPayInsApiService.removeById(id);
		
	}
	
	
	@Override
	public boolean modify(FncPrjPayInsVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.fncPrjPayInsApiService.modify(FncPrjPayInsVOTransform.toDTO(entity));
		
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		String sysCd=CurrentThreadContext.getCurrentSysCd();
		String orgCd=CurrentThreadContext.getCurrentOrgCd();
		List<KeyValueVO> list2= sysDictionaryApiService.getKvList(FncPrjPaySchConstant.FNC_PRJ_PAY_INS_ST_CD, sysCd, orgCd);
		page=this.fncPrjPayInsApiService.searchListPage(page);
		if (page.getResultList()!=null) {
			page.setResultList(FncPrjPayInsVOTransform.toVOList((List<FncPrjPayInsDTO>)page.getResultList()));
			for (int i=0;i< page.getResultList().size();i++) {
				FncPrjPayInsVO vo =((List<FncPrjPayInsVO>)page.getResultList()).get(i);
				for (KeyValueVO kv : list2) {
					if (kv.getKey().equals(vo.getStCd())) {
						vo.setStCdNm(kv.getValue());
						break;
					}
				}
			}
		}
		return page;
		
	}
	@Override
	public List<FncPrjPayInsVO> searchList(FncPrjPayInsQueryCondition qc ){
		
		List<FncPrjPayInsVO> list=FncPrjPayInsVOTransform.toVOList(this.fncPrjPayInsApiService.searchList(qc));
		String sysCd=CurrentThreadContext.getCurrentSysCd();
		String orgCd=CurrentThreadContext.getCurrentOrgCd();
		List<KeyValueVO> list2= sysDictionaryApiService.getKvList(FncPrjPaySchConstant.FNC_PRJ_PAY_INS_ST_CD, sysCd, orgCd);
		
		if (list!=null) {
			for (int i=0;i< list.size();i++) {
				FncPrjPayInsVO vo =list.get(i);
				for (KeyValueVO kv : list2) {
					if (kv.getKey().equals(vo.getStCd())) {
						vo.setStCdNm(kv.getValue());
						break;
					}
				}
			}
		}
		return list;
	}
}
